package com.web.graduationpro.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.web.graduationpro.common.CommonDataClass;
import com.web.graduationpro.dao.service.ArticleService;
import com.web.graduationpro.dao.service.GoodsService;
import com.web.graduationpro.dao.service.InstrumentSchoolService;
import com.web.graduationpro.dao.service.TeacherService;
import com.web.graduationpro.model.InstrumentSchool;
import com.web.graduationpro.model.Teacher;
import com.web.graduationpro.util.PageUtil;

@Controller
@RequestMapping("/teacher/*")
public class TeacherController {

	@Resource(name="instrumentSchoolServiceImpl")
	private InstrumentSchoolService instrumentSchoolServiceImpl;
	
	@Resource(name="teacherServiceImpl")
	private TeacherService teacherServiceImpl;
	
	@Resource(name="goodsServiceImpl")
	private GoodsService goodsServiceImpl;
	
	@Resource(name="articleServiceImpl")
	private ArticleService articleServiceImpl;
	
	@RequestMapping(value = "/index", method = RequestMethod.GET)  
    public String index(HttpServletRequest request,HttpServletResponse reponse) {  
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		reponse.setContentType("text/html;charset=UTF-8");
		/*************************分页开始****************************/
		String actionUrl = "/GraduationPro/teacher/index.do?";
		int totalNum = 0;
		int showNum = 5;
		String currPageStr = request.getParameter("currPage");
		String pageSizeStr = request.getParameter("pageSize");
		int currPage = 1;
		if(currPageStr!=null && !"".equals(currPageStr.trim())){
			currPage = Integer.parseInt(currPageStr);
		}
		int pageSize = 12;
		if(pageSizeStr!=null && !"".equals(pageSizeStr.trim())){
			pageSize = Integer.parseInt(pageSizeStr);
		}
		String queryCountHql = "SELECT count(*) From Teacher t WHERE t.id!=0 ";
		totalNum = teacherServiceImpl.findTotalCount(queryCountHql);
		List<String> page = null;
		if(totalNum>0){
			page = PageUtil.getInstance().getPage(actionUrl, totalNum, showNum, currPage, pageSize);
		}
		request.setAttribute("page", page);
		/*************************分页结束****************************/
		
		/*************************查询数据开始****************************/
		String hql = "  FROM Teacher t WHERE t.id!=0";
		List<Teacher> queryTeachers = teacherServiceImpl.findByPage(hql, ((currPage-1)*pageSize), pageSize);
		if(queryTeachers!=null && queryTeachers.size()!=0){
			request.setAttribute("queryTeachers", queryTeachers);
		}

		new CommonDataClass(instrumentSchoolServiceImpl,teacherServiceImpl,goodsServiceImpl,articleServiceImpl,request);
		/*************************查询数据结束****************************/
		
		return "/teacher";
    }  
	
	@RequestMapping(value = "/info", method = RequestMethod.GET)  
    public String info(HttpServletRequest request,HttpServletResponse reponse) {  
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		reponse.setContentType("text/html;charset=UTF-8");
		
		String id = request.getParameter("id");
		if(id!=null && !"".equals(id.trim())){
			Teacher teacher = (Teacher)teacherServiceImpl.findById(Teacher.class, Integer.parseInt(id));
			request.setAttribute("teacher", teacher);
		}
		new CommonDataClass(instrumentSchoolServiceImpl,teacherServiceImpl,goodsServiceImpl,articleServiceImpl,request);
        
		return "/teacherinfo";
    }  
}
